/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.bsu.android.gtv.apps.bconnected.data;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;

import edu.bsu.android.gtv.apps.bconnected.objects.Connection;
import edu.bsu.android.gtv.apps.bconnected.parser.ParserFactory;
import edu.bsu.android.gtv.apps.bconnected.parser.ServiceParser;
import edu.bsu.android.gtv.apps.bconnected.util.AnalyticsUtils;
import edu.bsu.android.gtv.apps.bconnected.util.BSUPrivate;
import edu.bsu.android.gtv.apps.bconnected.util.Constants;
import edu.bsu.android.gtv.apps.bconnected.util.Crypto;

/**
 * Data layer for the {@link Connection} object
 * @author Kyle Parker, University Computing Services
 */
public class ConnectionData {
    private static ArrayList<Connection> mConnections;
    
    /**
     * Insert the connection info using a | separated input parameter containing one or more connections, depending on whether it is 
     * called from the HomeActivity during the export or from the other detail fragments when viewing a single individual
     * @param bsuID
     * @param loginGuid
     * @param input
     * @param activity
     * @throws UnsupportedEncodingException
     */
    public static void insertConnection(String bsuID, String loginGuid, String input, Activity activity) 
    		throws UnsupportedEncodingException {
    	List<String> querystringParms = new ArrayList<String>(4);

		querystringParms.add(Constants.QUERYSTRING_BSU_ID + URLEncoder.encode(bsuID, "UTF-8"));
		querystringParms.add(Constants.QUERYSTRING_LOGIN_GUID + loginGuid);
		querystringParms.add(Constants.QUERYSTRING_INPUT + URLEncoder.encode(input, "UTF-8"));

		ServiceParser parser = ParserFactory.getParser(Constants.SERVICE_TYPE_SAVE_CONNECTION, querystringParms, activity);
		mConnections = parser.parseConnection();
    }

    /**
     * Using a new thread, separate from the main activity, call the service handler to insert the connection
     * @param bsuID
     * @param loginGuid
     * @param type
     * @param isChecked
     * @param activity
     */
    public static void insertConnection(final String bsuID, final String loginGuid, final String connectionBSUID, final int type,
    		final boolean isChecked,  final Activity activity) {			
        Runnable load = new Runnable(){
            public void run() { 
            	try {
            		Crypto c = new Crypto(BSUPrivate.KEY);
            		int checked = isChecked? 1 : 0;
            		
            		String input = connectionBSUID + "-" + type + "-" + checked + "|";
            		
    	            insertConnection(bsuID, loginGuid, c.encryptAsBase64(input.getBytes()), activity);
            	} catch (Exception ex) {
                    AnalyticsUtils.getInstance(activity).trackPageView(ex.getMessage());
            	}
            }
        };
        
        Thread thread = new Thread(null, load, "insertConnection");
        thread.start();
    }

    /**
     * Returns the integer value of the {@link Connection} object size
     * @return mConnections.size() | 0
     */
    public static int getConnectionCount() {
    	if (mConnections == null) {
    		return 0;
    	} else {
            return mConnections.size();
    	}
    }

    /**
     * Returns a {@link Connection} object at the index specified
     * @param i - the index of the object to be returned
     * @return mConnections.get(i)
     */
    public static Connection getConnection(int i) {
        return mConnections.get(i);
    }

    /**
     * Returns the ArrayList<Connection>
     * @return mConnections
     */
    public static ArrayList<Connection> getConnections() {
        return mConnections;
    }
}
